const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const merge = require('webpack-merge');
const webpackBaseConfig = require('./webpack.base.config.js');
// const fs = require('fs');
const path = require('path');

// @link https://vue-loader.vuejs.org/zh/#vue-loader-%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F
// const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HappyPack = require('happypack');

// fs.open('./src/config/env.js', 'w', (err, fd) => {
// 	const buf = 'export default "development";';
// 	/* eslint-disable-next-line */
// 	fs.write(fd, buf, 0, buf.length, 0, (err, written, buffer) => {});
// 	fs.close(fd);
// });

module.exports = merge(webpackBaseConfig, {
	devtool: 'inline-source-map',
	output: {
		publicPath: '/dev-wx/',
		filename: '[name].js',
		chunkFilename: '[name].chunk.js',
	},
	plugins: [
		// @see https://github.com/amireh/happypack
		new HappyPack({
			id: 'babel',
			threads: 4,
			loaders: ['babel-loader?cacheDirectory=true'],
		}),

		// @link https://github.com/webpack-contrib/mini-css-extract-plugin
		new MiniCssExtractPlugin({
			filename: '[name].[hash].css',
			chunkFilename: '[id].[hash].css',
		}),
		// new ExtractTextPlugin({
		// 	filename: '[name].css',
		// 	allChunks: true,
		// }),
		new HtmlWebpackPlugin({
			filename: 'index.html',
			template: './src/template/index.ejs',
			inject: true,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['/js/vconsole.min.js'],
			publicPath: false,
			append: false,
			hash: false,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['https://res.wx.qq.com/open/js/jweixin-1.2.0.js'],
			publicPath: false,
			append: false,
			hash: false,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['/static/vendors.wx-system.js'],
			publicPath: false,
			append: false,
			hash: true,
		}),
		new webpack.DefinePlugin({
			'process.env': {
				DEV: 'true',
			},
		}),
		new webpack.DllReferencePlugin({
			context: __dirname,
			/* eslint-disable-next-line import/no-dynamic-require */
			manifest: require(path.join(__dirname, '.', 'manifest.json')),
		}),
		new webpack.optimize.ModuleConcatenationPlugin(),
	],

	devServer: {
		disableHostCheck: true,

		host: 'localhost',
		// publicPath: '/wx-system/dist/',
		port: 8966,
		// https: true,
		open: false,
		// 让浏览器打开本机IP，而不是 host 的值
		useLocalIp: true,
		// 自动打开页面的路径, 不能写网址，前面也不要写 /
		openPage: 'dev-wx/index.html',

		compress: true, // gzip 压缩
	},
});
